package com.gxgeek.jucutil;

import java.util.Map;
import java.util.concurrent.*;

/**
 * ClassName: <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:   TODO ADD REASON. <br/>
 * Date:     2017/12/26 下午9:35 <br/>
 *
 * @author test
 * @see
 * @since JDK
 */
public class BankWaterService  implements Runnable{

    private CyclicBarrier cyclicBarrier = new CyclicBarrier(4, this);

    private Executor executor = Executors.newFixedThreadPool(4);

    private ConcurrentHashMap<String, Integer> concurrentHashMap = new ConcurrentHashMap<>();

    private void  count(){
        for (int i = 0; i < 4; i++) {
            executor.execute(()->{
                System.out.println( Thread.currentThread().getName()+" run");

                concurrentHashMap.put(Thread.currentThread().getName(), 1);
                try {
                    cyclicBarrier.await();
                }catch (Exception e){
                    e.printStackTrace();

                }

            });
        }
    }
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+ "this run");
        int result = 0;
        for (Map.Entry<String,Integer> sheet:concurrentHashMap.entrySet()){
            result += sheet.getValue();
        }
        concurrentHashMap.put("result", result);
        System.out.println(result);
    }

    public static void main(String[] args) {
        BankWaterService bankWaterService = new BankWaterService();
        bankWaterService.count();
//        bankWaterService.cyclicBarrier

    }

}
